import {Component, OnInit, Input} from '@angular/core';
import {TreeNode} from 'primeng/primeng';
import {MdlSnackbarService} from '@angular-mdl/core';
import {ConfigService} from './cfg.service';

@Component({
    selector: 'tabs',
    templateUrl: './tabs.component.html',
    styleUrls: ['./tabs.component.scss'],
    providers: [ConfigService]
})
export class TabsComponent implements OnInit {
    id: number;
    loading = true;
    scrollHeight: string = '450px';
    procActTree: TreeNode[] = [];
    selectedNode: any;

    tabs: any[] = []; //标签页项
    tabMenus: any[] = []; //标签页菜单项
    parts: [] = []; //参与人
    selectedPart: any; //当前选中参与者
    selectedTab: any; //当前选中标签页
    selectedTabMenu: any; //当前选中标签页菜单
    allTabSelected: number = 0;
    allTabMenuSelected: number = 0;

    selectedTabs: any[] = []; //选中的标签页项
    selectedTabMenus: any[] = []; //选中的标签页菜单项
    selectedPartId: any; //当前选中参与者ID
    selectedActDefId: any; //当前选中参与者ID


    @Input()
    bizId: number = 0;

    @Input()
    systemUrl: string = '';

    @Input()
    set taskListId(taskListId: number) {
        if (!taskListId || !this.bizId) {
            return;
        }
        this.id = taskListId;
        //加载菜单组
        this.configService.loadTabMenus(this.systemUrl).subscribe(tabMenus => {
            this.tabMenus = tabMenus;
            //初始化流程、环节、参与者
            this.configService.getProcActPartTreeByBizId(this.bizId, this.currentProcDefId, this.systemUrl).subscribe(data => {
                this.loading = false;
                this.procActTree = data;
                if (this.procActTree && this.procActTree.length > 0) {
                    this.selectedNode = this.procActTree[0]['children'][0];
                    this.nodeSelect(null);
                }
            });
        });
    }

    get taskListId(): number {
        return this.id;
    }

    currentProcDefId: number = 0;
    @Input()
    set procDefId(procDefId: number){
        if(!procDefId)return;
        this.currentProcDefId = procDefId;
        //加载菜单组
        this.configService.loadTabMenus(this.systemUrl).subscribe(tabMenus => {
            this.tabMenus = tabMenus;
            //初始化流程、环节、参与者
            this.configService.getProcActPartTreeByBizId(this.bizId, this.currentProcDefId, this.systemUrl).subscribe(data => {
                this.loading = false;
                this.procActTree = data;
                if (this.procActTree && this.procActTree.length > 0) {
                    this.selectedNode = this.procActTree[0]['children'][0];
                    this.nodeSelect(null);
                }
            });
        });
    }

    constructor(
        private configService: ConfigService,
        private mdlSnackbarService: MdlSnackbarService) {
    }

    ngOnInit() {
    }

    /**节点选中事件 */
    nodeSelect($event) {
        this.parts = this.selectedNode.data.parts;
        this.selectedPart = this.selectedNode.data.parts[0];
        this.selectedPartId = this.selectedNode.data.parts[0].partId;
        this.selectedActDefId = <number>this.selectedNode.id.substring(2);
        //this.onPartRowSelect($event);
        this.getTabList(this.id, this.selectedPartId, this.selectedActDefId);
    }


    getTabList(taskListId, partId, actDefId) {
        this.configService.getTabList(taskListId, actDefId, partId, this.systemUrl).subscribe(tabs => {
            this.tabs = tabs;            
            if (tabs && tabs.length) {
                this.selectedTab = tabs[0];
                this.getTabMenuList(this.selectedTab.tabId);
            }
        });
    }

    getTabMenuList(id) {
        this.configService.getTabMenuList(this.id, this.selectedActDefId, this.selectedPartId, id, this.systemUrl).subscribe(tabMenus => {
            this.tabMenus = tabMenus;
        });
    }

    onTabRowSelect($event) {        
        this.getTabMenuList($event.data.tabId);
    }

    onTabMenuRowSelect($event) {
    }

    changeAllTabSelected($event) {
    }

    changeAllTabMenuSelected($event) {
    }

    tabChange($event) {
        if ($event.selected == false) {
            this.setTabs($event, 0);
        } else {
            this.setTabs($event, 1);
        }

    }

    setTabs(data, value) {
        let list = [];
        this.tabs.forEach(ele => {
            if (ele.tabId == data.tabId) {
                ele.selected = value;
            }
            list.push(ele);
        });
        this.tabs = list;
    }


    setTabMenus(data, value) {
        let list = [];
        this.tabMenus.forEach(ele => {
            if (ele.menuId == data.menuId) {
                ele.selected = value;
            }
            list.push(ele);
        });
        this.tabMenus = list;
    }


    tabMenuChange($event) {
        if ($event.selected == false) {
            this.setTabMenus($event, 0);
        } else {
            this.setTabMenus($event, 1);
        }
    }


    /** 选参与者 */
    onPartRowSelect($event) {
        if ($event) {
            this.selectedPartId = $event.data.partId;
            this.getTabList(this.id, this.selectedPartId, this.selectedActDefId);
        }
    }

    /**保存 */
    saveUpdate(event) {
        let param = {
            taskListId: this.id,
            actDefId: this.selectedActDefId,
            partId: this.selectedPartId,
            tabString: JSON.stringify(this.tabs)
        };
        this.configService.saveTabPart(param, this.systemUrl).subscribe(data => {
            let menuParam = {
                taskListId: this.id,
                actDefId: this.selectedActDefId,
                partId: this.selectedPartId,
                tabId: this.selectedTab.tabId,
                tabMenuString: JSON.stringify(this.tabMenus)
            };
            this.configService.saveTabMenuPart(menuParam, this.systemUrl).subscribe(data => {
                this.mdlSnackbarService.showToast("标签页权限配置成功");
            });
        });
    }

}
